var mongoose = require('mongoose'),
  conf = require('smart-conf')(),
  host = conf.get('mongo-admin:host'),
  port = conf.get('mongo-admin:port'),
  db = conf.get('mongo-admin:db'),
  connectionPath = 'mongodb://' + host + ':' + port + '/' + db;

/* Single connection to DataBase. */
if (mongoose.connection.readyState === 0) {
  mongoose.connect(connectionPath, function (err) {
    if (err) {
      // at server startup, if mongo can't be reached, print something useful before the server crashed
      console.error('Can\'t connect to mongo (' + connectionPath + '): ' + err.message);
      //if mongo stops while web server is up, throw the error each time someone will try to connect
      throw new Error({
        message: 'Can\'t connect to mongo (' + connectionPath + '): ' + err.message,
        code: err.code
      });
    }
  });
}
